package com.example.dp.myapp;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    public final String TAG = "kkww";
    Button btn;
    //由AIDL文件生成的Java类
    private BookManager mBookManager = null;

    //标志当前与服务端连接状况的布尔值，false为未连接，true为连接中
    private boolean mBound = false;

    private List<Book> mBooks;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.btn_main);
        btn.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!mBound) {
//            attemptToBindService();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mBound) {
            unbindService(mServiceConnection);
            mBound = false;
        }
    }

    @Override
    public void onClick(View v) {
        addBook();
    }

    private void addBook() {

        if (!mBound) {
            attemptToBindService();
            return;
        }

        if (mBookManager == null) return;
        Book book = new Book();
        book.setName("APP研发录In");
        book.setPrice(30);
        Toast.makeText(this, "开始插入", Toast.LENGTH_SHORT).show();
        try {
            mBookManager.addBook(book);
            Log.e(TAG, book.toString());
        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }

    private void attemptToBindService() {
        Toast.makeText(this, "开始连接", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent();
        intent.setAction("com.example.dp.myapp.aidl");
        intent.setPackage("com.example.dp.myappserver");
        bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
    }

    private ServiceConnection mServiceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e(TAG, "service connected");
            Toast.makeText(MainActivity.this, "连上了", Toast.LENGTH_SHORT).show();
            mBookManager = BookManager.Stub.asInterface(service);
            mBound = true;
            if (mBookManager != null) {
                try {
                    mBooks = mBookManager.getBooks();
                    Log.e(TAG, mBooks.toString());
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e(TAG, "service disconnected");
            Toast.makeText(MainActivity.this, "没连上", Toast.LENGTH_SHORT).show();
            mBound = false;
        }
    };
}
